package com.webwork.lims.system.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.webwork.lims.common.controller.SuperCtrl;
import com.webwork.lims.system.dataobject.Role;
import com.webwork.lims.system.dataobject.User;
import com.webwork.lims.system.service.UserService;
import com.webwork.lims.util.Paging;
import com.webwork.lims.util.RSA;
import com.webwork.lims.util.StrManager;

@Controller
@RequestMapping(value = "/system")
public class UserController extends SuperCtrl {

	@Autowired
	private UserService userSve;
	
	@RequestMapping(value = "/users")
	@Override
	public String mainPage() {
		return "users";
	}
	
	@RequestMapping(value = "queryUsersList")
	@ResponseBody
	public ResponseEntity<String> queryList(
			@RequestParam(value = "page") int page,
			@RequestParam(value = "rows") int rows,
			@RequestParam(required = false, value = "cmd") String cmdStr)
					throws Exception {
		User queryObj = new User();
		if (!StrManager.isEmpty(cmdStr)) {
			queryObj = new Gson().fromJson(cmdStr, User.class);	
			queryObj.setRole(queryObj.getRoleCode());
		}
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Map<String, Object> ret = new HashMap<String, Object>();
	
		ret.put("total", userSve.count(queryObj));
		ret.put("rows", userSve.query(queryObj, new Paging(page, rows)));
		
		return new ResponseEntity<String>(new Gson().toJson(ret).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
	@RequestMapping(value = "getUser")
	@ResponseBody
	public ResponseEntity<String> getUser(@RequestParam("userId") String userId) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		User user = userSve.get(userId);
		user.setRoleCode(user.getRole().getCode());
		return new ResponseEntity<String>(new Gson().toJson(user).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
	@RequestMapping(value = "addUser")
	@ResponseBody
	public String addUser(@RequestParam("jsonObj") String jsonObj) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		User user = new Gson().fromJson(jsonObj, User.class);
		if (userSve.get(user.getUserId()) != null) {
			return EXIST;
		}
		user.setRole(user.getRoleCode());
		user.setRegistDate(new Date());
		user.setPassword(RSA.encodeBase("555"));	//密码默认为555
		userSve.save(user);
		return SUCCESS;
	}
	
	@RequestMapping(value = "editUser")
	@ResponseBody
	public String editUser(@RequestParam("jsonObj") String jsonObj) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		User o = new Gson().fromJson(jsonObj, User.class);
		User user = userSve.get(o.getUserId());
		if (user == null) {
			return NOT_EXIST;
		}
		user.setUserName(o.getUserName());
		user.setRole(o.getRoleCode());
		userSve.save(user);
		return SUCCESS;
	}
	
	@RequestMapping(value = "delUser")
	@ResponseBody
	public String delUser(@RequestParam("userId") String userId) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		User user = userSve.get(userId);
		if (user == null) {
			return FAIL;
		} else if (user != null && user.getRole() == Role.ADMIN) {
			return "admin";
		} else if (user != null && user.getRole() == Role.CUSTOMER) {
			return "guest";
		}
		userSve.delete(userId);
		return SUCCESS;
	}
	
	@RequestMapping(value = "editPassword")
	@ResponseBody
	public String editPassword(@RequestParam("oldPwd") String oldPwd, @RequestParam("newPwd") String newPwd,
			HttpServletRequest request) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		User user = (User) request.getSession(true).getAttribute("user");
		if (!user.getPassword().equals(RSA.encodeBase(oldPwd))) {
			return FAIL;
		}
		user.setPassword(RSA.encodeBase(newPwd));
		userSve.update(user);
		return SUCCESS;
	}
	
	@RequestMapping(value="loadRole")
	@ResponseBody
	public ResponseEntity<String> loadRole() {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Role[] roles = Role.useableValues();
		List<Object> ret = new ArrayList<Object>();
		for (Role o : roles) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("value", o.getCode());
			map.put("name", o.getCode() + " - " + o.getMsg());
			ret.add(map);
		}
		return new ResponseEntity<String>(new Gson().toJson(ret).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
	@RequestMapping(value="loadDelivery")
	@ResponseBody
	public ResponseEntity<String> loadDelivery() {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		User userQuery = new User();
		userQuery.setRole(Role.DELIVERY);
		List<User> lists = userSve.query(userQuery);
		List<Object> ret = new ArrayList<Object>();
		for (User o : lists) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("value", o.getUserId());
			map.put("name", o.getUserId() + " - " + o.getUserName());
			ret.add(map);
		}
		return new ResponseEntity<String>(new Gson().toJson(ret).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
}
